
//格式化时间
function CurentTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hh = date.getHours();
    var mm = date.getMinutes();
    var clock = year + "-";
    if (month < 10)
        clock += "0";
    clock += month + "-";
    if (day < 10)
        clock += "0";
    clock += day + " ";
    if (hh < 10)
        clock += "0";
    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    return clock;
}
//格式化时间
function CurentTimeWithSeconds(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hh = date.getHours();
    var mm = date.getMinutes();
    var ss = date.getSeconds();
    var clock = year + "-";
    if (month < 10)
        clock += "0";
    clock += month + "-";
    if (day < 10)
        clock += "0";
    clock += day + " ";
    if (hh < 10)
        clock += "0";
    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm + ":";
    if (ss < 10) clock += "0";
    clock += ss;
    return clock;
}

//跳转到底部
function end() {
    var c = window.document.body.scrollHeight;
    window.scroll(0, c);
}

$(function () {
    $('.footer').on('keyup','input',function(){
        if($(this).val().length>0){
            $(this).next().css('background','#114F8E').prop('disabled',true);

        }else{
            $(this).next().css('background','#ddd').prop('disabled',false);
        }
    })

    //当前用户,即为发送者
    var senderId = JSON.parse(sessionStorage.getItem("user")).id;
    var senderHeadImgUrl = JSON.parse(sessionStorage.getItem("user")).headImgUrl;

    //接收者id
    var receiverId = getParams().receiverId;

    $.get("/users/"+receiverId,function (data) {
        $("#sendImg").attr("src",data.headImgUrl);
        $(".receiverName").html(data.nickName);
        $(".receiverData").click(function () {
            window.location.href="/userProfiles.html?id="+receiverId;
        });
    });
    var messagesModal = [];
    //发送请求,通过发送者id和接受者id获取到两者之间的信息
    $.get("/messages/"+senderId+"/"+receiverId+"/chart",function (data) {
        $.merge(messagesModal,data);
        console.log(messagesModal);
        //渲染数据
        $("#messagesModal").renderValues({list:messagesModal},{
            getClass:function (item,value) {
                if (value == senderId) {
                    $(item).removeClass("send").addClass("show");
                }
            },
            getHref:function (item,value) {
                $(item).attr("src",value);
            },
            getMessage:function (item,value) {
                $(item).html("<i class=\"msg_input\"></i>"+value);
            }
        });
        end()
    });

    //给发送按钮绑定点击事件
    $("#sendBtn").click(function () {
        var message = $("[name='message']").val();
        if (message == ""){
            //如果没有信息,直接返回
            $(document).dialog({
                type : 'notice',
                content: '<span class="info-text">内容不能为空</span>',
                autoClose: 1000,
                position: 'bottom'
            });
            return;
        }
        //将发送的聊天持久化到数据库
        $.ajax({
            url :"/messages",
            data:{"sender.id":senderId,"recipient.id":receiverId,"message":message},
            type:"POST",
            success:function (data) {
                console.log(data);
                //回显发送的聊天信息
                $.merge(messagesModal,data);
                //渲染数据
                $("#messagesModal").renderValues({list:messagesModal},{
                    getClass:function (item,value) {
                        if (value == senderId) {
                            $(item).removeClass("send").addClass("show");
                        }
                    },
                    getHref:function (item,value) {
                        $(item).attr("src",value);
                    },
                    getMessage:function (item,value) {
                        $(item).html("<i class=\"msg_input\"></i>"+value);
                    }
                });
                end();
            }
        });
        //发送完后清空信息框内容
        $("[name='message']").val("");
        $("[name='message']").next().css('background', '#ddd').prop('disabled', false);
    });
    /*//查看这个页面的时候设置这条信息状态为已读状态
    $.ajax({
        type: "PUT",
        url: "/userChats/"+senderId+"/"+receiverId
    });*/
    var newTime = CurentTimeWithSeconds(new Date());
   var flag = true;//设置查询标志
    function getNewMessage() {
        //
        /*if(!flag){
            //如果有新的消息,就刷新时间
            newTime = CurentTimeWithSeconds(new Date());
        }

        flag =false;*/
        $.post("/messages/newMessages",{senderId:senderId,state:false},
            function (data) {

                //如果有新数据
            if (data.length > 0) {
                $.merge(messagesModal,data);
                //渲染数据
                $("#messagesModal").renderValues({list:messagesModal},{
                    getClass:function (item,value) {
                        if (value == senderId) {
                            $(item).removeClass("send").addClass("show");
                        }
                    },
                    getHref:function (item,value) {
                        $(item).attr("src",value);
                    },
                    getMessage:function (item,value) {
                        $(item).html("<i class=\"msg_input\"></i>"+value);
                    }
                });
                end();
            }
                window.setTimeout(getNewMessage, 1000);
        });

    }
    //
    getNewMessage();
});
